package it.traveldream.entities;

import it.traveldream.dto.TransportMeanDTO;

import java.math.BigDecimal;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 * Entity implementation class for Entity: TrasportMean
 *
 */
@Entity
@Table(name="TransportMean")
@DiscriminatorValue(ProductType.TRANSPORTMEAN)

@NamedQueries({
@NamedQuery(
		name = "getAllMeanToLocation",
		query = "SELECT m FROM TransportMean m WHERE m.location = :location"
		),
@NamedQuery(
		name = "getAlternativeMeanToLocation",
		query = "SELECT m FROM TransportMean m WHERE m.location = :location AND "
				+ "m.productId != :id"
		),
@NamedQuery(
		name = "getExistingBackMeanOfTypeToLocation",
		query = "SELECT m FROM TransportMean m WHERE m.meanType = :type AND "
				+ "m.location = :location"
		),
@NamedQuery(
		name = "getExistingMeanFromDepartureToLocation",
		query = "SELECT m FROM TransportMean m WHERE m.departure = :departure "
				+ " AND m.location = :location"
		)
})
public class TransportMean extends Product  {

	
	private int spaces;
	private BigDecimal price;
	private static final long serialVersionUID = 1L;

	@ManyToOne(optional = false)
	@JoinColumn(name = "MEANTYPEID", referencedColumnName = "MEANTYPEID", nullable = false)
	private MeanType meanType;
	
	@ManyToOne(optional = false)
	@JoinColumn(name = "DEPARTUREID", referencedColumnName = "DEPARTUREID", nullable = false)
	private Departure departure;
	
	public TransportMean() {
		super();
	}
	
	public TransportMean(String name, Location location) {
		super();
		this.name = name;
		this.location = location;
	}

	public int getSpaces() {
		return this.spaces;
	}

	public void setSpaces(int spaces) {
		this.spaces = spaces;
	}   
	public BigDecimal getPrice() {
		return this.price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public MeanType getMeanType() {
		return this.meanType;
	}

	public void setMeanType(MeanType meanType) {
		this.meanType = meanType;
	}

	public Departure getDeparture() {
		return this.departure;
	}

	public void setDeparture(Departure departure) {
		this.departure = departure;
	}

	@Override
	public TransportMeanDTO toDTO(){
		TransportMeanDTO dto = new TransportMeanDTO();
		dto.setProductId(this.productId);
		dto.setName(this.name);
		dto.setLocation(this.location.toDTO());
		dto.setSpaces(this.spaces);
		dto.setPrice(this.price);
		dto.setDeparture(this.departure.toDTO());
		dto.setMeanType(this.meanType.getName());
		
		return dto;
	}
   
}
